// JavaScript Document
var categoryDTOS;
$(function(){
    // 初始化报销数据
    var userInfo = doAjaxOnlyUrl(user_info_url);
    if (userInfo.roleId == 2){
        $(".header ul li:eq(2) a").css('display','block');
    }
    var initData = doAjaxOnlyUrl(expense_apply_init_url);
    var expenseStoreList = initData.expenseStoreList;
    categoryDTOS = initData.categoryDTOS;
    var goodsSelectOneStr = "<select onchange='selectOne(this)'><option>一级菜单</option>";
    var goodsSelectTwoStr = "<select ><option>二级菜单</option></select>";
    for (var i = 0; i < categoryDTOS.length; i++){
        goodsSelectOneStr = goodsSelectOneStr + "<option value="+categoryDTOS[i].id+">"+categoryDTOS[i].name+"</option>";
    }
    goodsSelectOneStr = goodsSelectOneStr + "</select>";

    var storeSelectStr = "<select ><option value='0'>请选择</option>";
    for (var i = 0; i < expenseStoreList.length; i++){
        storeSelectStr = storeSelectStr + "<option value="+expenseStoreList[i].id+">"+expenseStoreList[i].name+"</option>";
    }
    storeSelectStr = storeSelectStr + "</select>";

    // 设置申请店铺和申请人员
    $(".header ul li:eq(0) span").html(initData.userName);
    if(initData.toConfirmNum != null && initData.toConfirmNum != 0){
        $(".header ul li:eq(3) a").css('display','block');
    }
    //报销申请
    $('.add-btn').click(function(){
        var tr ="<tr name='expenseData'>"+
            "<td class=\"txt\">"+goodsSelectOneStr+goodsSelectTwoStr+"</td>"+
            "<td class=\"txt\"><input></td>"+
            "<td class=\"txt\">"+storeSelectStr+"</td>"+
            "<td class=\"txt\"><input></td>"+
            "<td class=\"txt\"><a href=\"javascript:;\" onclick=\"deleteLine(this)\">删除<input></td>"+
            "</tr>"
        $('#Apply').append(tr);

    });


    /*
    // 设置接收店铺和确认人员数据
    var store = $("#store");
    store.prepend("<option value='0'>请选择</option>");
    var user = $("#user");
    user.prepend("<option value='0'>请选择</option>");
    for(var i = 0; i < storeList.length; i ++){
        store.append("<option value="+storeList[i].id+" userName="+storeList[i].userName+">"+storeList[i].name+"</option>");
        user.append("<option storeId="+storeList[i].id+">"+storeList[i].userName+"</option>");
    }
    // 接收店铺和确认人员下拉框联动
    $("#store").change(function () {
        var store = $(this).children('option:selected');
        var userName = store.attr("userName");
        $("#user option").each(function () {
           var text = $(this).text();
           if(text == userName){
               $(this).attr("selected", true);
               return;
           }
        });

    });
    $("#user").change(function () {
        var user = $(this).children('option:selected');
        if(store.text() == "请选择"){
            return;
        }
        var storeId = user.attr("storeId");
        $("#store option").each(function () {
            var id = $(this).val();
            if(id == storeId){
                $(this).attr("selected", true);
                return;
            }
        });
    });*/
    // 确认报销
    $(".rit-btn").click(function () {
        if(confirm("确认提交报销申请吗") == true){
            var applyList = new Array();
            var trs = $("tr[name='expenseData']");
            if(trs.length == 0){
                alert("请填写报销数据");
                return;
            }
            var flag = 0;
            trs.each(function (i) {
                var obj = {};
                console.log($(this).children('td').eq(0).find('option:selected').text());
                obj.goodsName = $(this).children('td').eq(0).children('select').eq(1).find("option:selected").text();
                if(obj.goodsName == "二级菜单"){
                    flag = 1;
                    alert("请选择报销名称");
                    return false;
                }
                obj.price = $(this).children('td').eq(1).children('input').val();
                if(obj.price == null || obj.price.trim() == ''){
                    flag = 1;
                    alert("总价不能为空");
                    return false;
                }
                obj.expenseStoreId = $(this).children('td').eq(2).find('option:selected').val();
                if(obj.expenseStoreId == 0){
                    flag = 1;
                    alert("请选择审核店铺");
                    return false;
                }
                var remark = $(this).children('td').eq(3).children('input').val();
                if(remark.length > 30){
                    flag = 1;
                    alert("备注不能超过30个字符");
                    return false;
                }
                obj.remark = remark;
                applyList[i] = obj;
            });
            if(flag){
                return;
            }
            var data = {};
            data.applyList = JSON.stringify(applyList);
            var result = doAjaxAll(expense_apply_confirm, data);
            if(result.code == 1){
                alert("申请成功！");
                location.reload();
            }

        }
    });
});
function goToConfirm() {
    location.href = expense_apply_confirm_html;
}
function goToHistory() {
    location.href = expense_apply_His_html;
}
function goToConfirmHistory(){
    location.href = expense_apply_confirm_His_html;
}
function selectOne(obj) {
    var id = $(obj).val();
    var selectTwo = $(obj).next();
    var categoryDTOTmp;
    for (var i = 0; i < categoryDTOS.length; i++){
        if (categoryDTOS[i].id == id){
            categoryDTOTmp = categoryDTOS[i].categoryDTOS;
            break;
        }
    }
    selectTwo.empty();
    selectTwo.prepend("<option>二级菜单</option>")
    for (var i = 0; i < categoryDTOTmp.length; i++){
        selectTwo.append("<option value="+categoryDTOTmp[i].id+">"+categoryDTOTmp[i].name+"</option>");
    }
}
function deleteLine(obj) {
    $(obj).parent().parent().remove();
}


























